package com.sbwork.demo.service;

import com.sbwork.base.service.BaseService;
import com.sbwork.demo.convert.HyProjectConvert;
import com.sbwork.demo.entity.HyProject;
import com.sbwork.demo.form.HyProjectForm;
import com.sbwork.demo.persistence.HyProjectMapper;
import com.sbwork.demo.searchForm.HyProjectSearchForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * projectId Service 类
 */
@Service
public class HyProjectService implements BaseService<HyProjectSearchForm, HyProjectForm> {
    /**
     * 自动注入HyProjectMapper
     */
    @Resource
    private HyProjectMapper hyProjectMapper;

    /**
     * 自动注入HyProjectConvert
     */
    @Resource
    private HyProjectConvert hyProjectConvert;

    /**
     * 按projectId进行查询
     * @param projectId 项目标识
     * @return 项目管理form
     */
    public HyProjectForm getByProjectId(String projectId) {
        HyProject entity = hyProjectMapper.getByKeyId(projectId);
        return hyProjectConvert.convertToForm(entity);
    }

    /**
     * 按列表进行查询，后回项目管理form的集合
     * @param searchForm 项目管理searchForm
     * @return 项目管理form的集合
     */
    @Override
    public List<HyProjectForm> listPageBySearchForm(HyProjectSearchForm searchForm) {
        List<HyProject> list = hyProjectMapper.getBySearchForm(searchForm);
        return hyProjectConvert.convertToFormList(list);
    }

    /**
     * 更新操作，当项目标识不存在的时候，直接插入，否则，进行更新
     * @param form 项目管理form
     * @return 更新后的form
     */
    @Override
    public HyProjectForm commit(HyProjectForm form) {
        HyProject entity = hyProjectConvert.convertToEntity(form);
        if(StringUtils.isEmpty(entity.getProjectId())){
            hyProjectMapper.insert(entity);
        }else{
            hyProjectMapper.update(entity);
        }
        return hyProjectConvert.convertToForm(entity);
    }

    /**
     * 更新顺序
     *
     * @param keyIds 项目标识S
     */
    public void updateOrder(String[] keyIds) {
        for (int i = 0; i < keyIds.length; i++) {
            String keyId = keyIds[i];
            HyProject entity = this.hyProjectMapper.getByKeyId(keyId);
            entity.setSortNo(i + 1);
            this.hyProjectMapper.update(entity);
        }
    }

    /**
     * 按projectIds 删除 项目管理
     * @param projectIds projectId集合
     */
    @Override
    public void remove(String... projectIds) {
        for(String projectId : projectIds){
            hyProjectMapper.deleteByKeyId(projectId);
        }
    }
}
